<template>
    <div class="project-info-add">
        <ft-dialog :visible.sync="dialogVisible" width="600px" title="发起项目评审" height="297px" @close="onCancel">
            <el-form ref="dataRef" v-if="dialogVisible" class="project-info-add_form" :model='entityData'
                     size='small' inline :rules="rules" v-loading="loading">
                <el-row>
                    <el-col>
                        <ft-form-item label='项目名称' show-overflow-tooltip prop='projectApprovalId'>
                            <ft-project :title="entityData.projectName" show-overflow-tooltip v-model='entityData.projectApprovalId'  @change="onChange"/>
                        </ft-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <ft-form-item label='项目编号' prop='projectCode'>
                            <span v-if="entityData.projectCode">{{entityData.projectCode}}</span>
                            <span v-else>请输入</span>
                        </ft-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <ft-form-item label='客户名称' prop='customerName'>
                            <span v-if="entityData.customerName">{{entityData.customerName}}</span>
                            <span v-else>请输入</span>
                        </ft-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                      <ft-form-item label='租赁类型' prop='productMinEm'>
                        <ft-enum-select
                            v-if="entityData.productMinEm"
                            v-model="entityData.productMinEm"
                            service-name="ncls-bms"
                            type-name="productMin"
                            :multiple="false"
                            clearable
                            :editing='false'
                            size="small"/>
                        <span v-else>请选择租赁类型</span>
                      </ft-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <ft-form-item label='项目金额' prop='projectAmt'>
                            <ft-number-input-unit v-model='entityData.projectAmt'
                                                  :percentUtil="true"
                                                  :companyUnit="amountUnit"
                                                  :disabled = "true"
                                                  :precision='[14,2]'>
                            </ft-number-input-unit>
                        </ft-form-item>
                    </el-col>
                </el-row>
                <hr>
                <div class="form-floor_buttons">
                    <el-button size="small" @click="onCancel" :disabled="loading">取消</el-button>
                    <el-button class="save-button" type="primary" size="small" :disabled="loading" @click="onSave">保存
                    </el-button>
                </div>
            </el-form>
        </ft-dialog>
    </div>
</template>

<script>

    import FtProject from "./ft/FtProject";

    export default {
        name: "ProjectInfoAdd",
        components: {FtProject},
        props: {
          sourceBillType: {
            type: String,
            require: true
          }
        },
        data() {
          const validPreDepositAmt = (rule, value, callback) => {
            if (value <0) {
              callback(new Error('项目金额不能为负!'))
            } else {
              callback();
            }
          }
            return {
                dialogVisible: false,
                loading: false,
                dataProduct: [],
                entityData: {},
                amountUnit: '元',
                rules: {
                    projectApprovalId: [{required: true, message: '项目名称不能为空', trigger: "blur"}],
                    projectCode: [{required: true, message: '项目编号不能为空', trigger: ['blur','change']}],
                    customerName: [{required: true, message: '客户名称不能为空', trigger: ['blur','change']}],
                    productMinEm: [{required: true, message: '产品名称不能为空', trigger: "blur"}],
                    projectAmt: [{required: true, message: '项目金额不能为空', trigger: "blur"},{validator: validPreDepositAmt, trigger: ['blur','change']}]
                },
            }
        },
      mounted() {
        this.requestProduct()
      },
        methods: {
            onShowCancel(){
                this.dialogVisible = true
            },
            // 取消或返回
            onCancel() {
                this.dialogVisible = false
                this.entityData = {}
                this.$refs.dataRef.clearValidate()
                this.$emit("onCancel")
            },

            onChange(val) {
                this.$set(this.entityData, 'projectApprovalId', val.projectApprovalId);
                this.$set(this.entityData, 'projectCode', val.projectApprovalCode);
                this.$set(this.entityData, 'projectName', val.projectApprovalName);
                this.$set(this.entityData, 'customerName', val.customerName);
                this.$set(this.entityData, 'productMinEm', val.productMinEm);
                this.$set(this.entityData, 'projectAmt', val.projectApprovalAmt);
                this.$set(this.entityData, 'ifCircularLease', val.ifCircularLease);
                this.$set(this.entityData, 'projectCooperationPeriod', val.projectCooperationPeriod);
            },
            // 保存
            onSave() {
                // 防止重复操作
                if (this.loading) {
                    return
                }
                this.$refs.dataRef.validate(validate => {
                    if (validate) {
                        this.loading = true
                        this.entityData.sourceBillType = this.sourceBillType
                        let url = "/joys-rest/ncls-bms/project/save"
                        this.$http.post(url, this.entityData).then(resp => {
                            let projectId = resp.body.projectId
                            if (projectId) {
                                let params = {
                                    projectId: resp.body.projectId,
                                    billStatus: resp.body.billStatus,
                                    pageName: resp.body.projectName,
                                    jurisdictionRole: false
                                }
                                this.$emit('changeToDetails', params)
                                this.onCancel()
                            } else {
                                this.$message.error("新增失败！")
                            }
                        }, resp => {
                            if (resp.body) {
                                this.$message.error(resp.body.message || "新增失败！")
                            } else {
                                this.$message.error("新增失败！")
                            }
                        }).finally(() => {
                          this.loading = false
                        })
                    }
                })
            },

          requestProduct() {
            if (true) {
              this.$http.get('/joys-rest/ncls-bms/approval/product/list').then(resp => {
                this.dataProduct = resp.body
              }, resp => {
                this.$message.error(resp.body.message)
              })
            }
          },
        }
    }
</script>

<style lang="less">

    .el-select-dropdown{
      max-width: 391px !important;
    }
    .el-select-dropdown__item{
      overflow: visible;
    }
    .project-info-add {
        background-color: #FFFFFF;

        .project-info-add_return {
            margin: 12px 24px;
        }

        hr {
            border: none;
            height: 1px;
            margin: 0;
            background-color: rgba(179, 179, 179, 0.20);
        }

        .project-info-add_form {
            margin: 20px 24px 0;
            padding-top: 24px;
            padding-right: 24px;
            background-color: #FFFFFF;

            hr {
                border: none;
                height: 1px;
                margin:24px -71px 0 -48px;
                background-color: #e1e1e2;
            }

            .form-floor_buttons {
                width: 100%;
                height: 50px;
                display: flex;
                align-items: flex-end;
                justify-content: flex-end;

                .save-button {
                    margin-left: 16px;
                    margin-right: 30px;
                }

            }
        }
    }
</style>